package com.cleaningcloud.erp.controller;

import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.domain.ErpSupplierCommodity;
import com.cleaningcloud.erp.domain.vo.ErpCommodityVo;
import com.cleaningcloud.erp.domain.vo.ErpSupplierCommodityVo;
import com.cleaningcloud.erp.service.IErpSupplierCommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 供应商商品Controller
 *
 * @author cleaningcloud
 * @date 2024-09-26
 */
@RestController
@RequestMapping("/ErpSupplierCommodity")
public class ErpSupplierCommodityController extends BaseController {
    @Autowired
    private IErpSupplierCommodityService erpSupplierCommodityService;

    /**
     * 查询供应商商品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ErpSupplierCommodity erpSupplierCommodity) {
        startPage();
        List<ErpSupplierCommodityVo> list = erpSupplierCommodityService.selectErpSupplierCommodityList(erpSupplierCommodity);
        return getDataTable(list);
    }

    /**
     * 导出供应商商品列表
     */
    @RequiresPermissions("erp:ErpSupplierCommodity:export")
    @Log(title = "供应商商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpSupplierCommodity erpSupplierCommodity) {
        List<ErpSupplierCommodityVo> list = erpSupplierCommodityService.selectErpSupplierCommodityList(erpSupplierCommodity);
        ExcelUtil<ErpSupplierCommodityVo> util = new ExcelUtil<>(ErpSupplierCommodityVo.class);
        util.exportExcel(response, list, "供应商商品数据");
    }

    @Log(title = "供应商商品", businessType = BusinessType.IMPORT)
    @RequiresPermissions("erp:ErpSupplierCommodity:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, Long supplierId) throws Exception {
        ExcelUtil<ErpCommodityVo> util = new ExcelUtil<ErpCommodityVo>(ErpCommodityVo.class);
        List<ErpCommodityVo> commodityList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = erpSupplierCommodityService.importErpSupplierCommodity(commodityList, updateSupport, operName, supplierId);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<ErpCommodityVo> util = new ExcelUtil<ErpCommodityVo>(ErpCommodityVo.class);
        util.importTemplateExcel(response, "商品数据");
    }

    /**
     * 获取供应商商品详细信息
     */
    @GetMapping(value = "/{supplierCommodityId}")
    public AjaxResult getInfo(@PathVariable("supplierCommodityId") Long supplierCommodityId) {
        return success(erpSupplierCommodityService.selectErpSupplierCommodityBySupplierCommodityId(supplierCommodityId));
    }

    /**
     * 新增供应商商品
     */
    @RequiresPermissions("erp:ErpSupplierCommodity:add")
    @Log(title = "供应商商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpSupplierCommodity erpSupplierCommodity) {
        return toAjax(erpSupplierCommodityService.insertErpSupplierCommodity(erpSupplierCommodity));
    }

    /**
     * 修改供应商商品
     */
    @RequiresPermissions("erp:ErpSupplierCommodity:edit")
    @Log(title = "供应商商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpSupplierCommodity erpSupplierCommodity) {
        return toAjax(erpSupplierCommodityService.updateErpSupplierCommodity(erpSupplierCommodity));
    }

    /**
     * 删除供应商商品
     */
    @RequiresPermissions("erp:ErpSupplierCommodity:remove")
    @Log(title = "供应商商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplierCommodityIds}")
    public AjaxResult remove(@PathVariable Long[] supplierCommodityIds) {
        return toAjax(erpSupplierCommodityService.deleteErpSupplierCommodityBySupplierCommodityIds(supplierCommodityIds));
    }
}
